package com.sto.common.importExcel.dto.base;

import com.sto.common.importExcel.dto.response.ImportExcelResponse;
import lombok.Builder;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ChenPeng
 * @since 2022-07-15 13:42
 */
@Data
@Builder //@Builder注解：使用@Builder注解生成一个对象，并且可以链式书写为对象赋值
public class BaseImportExcelDto<T> {
    /**
     * 要映射的 Dto
     */
    private Class clzDto;

    /**
     * @Author: LiYuhang
     * @Date 2022/1/13 17:40
     */
    private Object excelListenerConsumer;

    /**
     * @param null
     * @return null
     * @Author: LiYuhang
     * @Date 2022/1/13 17:42
     */
    private Integer headRowNumber;

    /**
     * 导入完毕后，返回数据
     *
     * @param null
     * @return null
     * @Author: LiYuhang
     * @Date 2022/1/13 17:49
     */
    private volatile List afterImportData;

    /**
     * 返回数据
     */
    private ImportExcelResponse excelResponse = new ImportExcelResponse();

    private StringBuilder stringBuilder;

    /**
     * 数据初始化
     *
     * @param clzDto
     * @return
     */
    public static BaseImportExcelDto init(Class clzDto) {
        return init(clzDto, null);
    }

    /**
     * 数据初始化
     *
     * @param clzDto
     * @param headRowNumber
     * @return
     */
    public static BaseImportExcelDto init(Class clzDto, Integer headRowNumber) {
        BaseImportExcelDto<Object> build = BaseImportExcelDto.builder()
                .clzDto(clzDto).headRowNumber(headRowNumber)
                .stringBuilder(new StringBuilder())
                .build();
        // 如果返回数据为null 表示第一次执行，赋值，否则强转
        List resData = build.getAfterImportData() == null ? new LinkedList<>()
                : build.getAfterImportData();
        ImportExcelResponse excelResponse = build.getExcelResponse() == null ? new ImportExcelResponse()
                : build.getExcelResponse();
        build.setExcelResponse(excelResponse);
        build.setAfterImportData(resData);
        return build;
    }
}
